﻿using System;
using System.Linq;
using Microsoft.Phone.Controls;
using RapidRepository.TestMobileApplication.Repositories;
using RapidRepository.TestMobileApplication.Entities;
using System.Windows;

namespace RapidRepository.TestMobileApplication
{
    public partial class EditCustomer : PhoneApplicationPage
    {
        public EditCustomer()
        {
            InitializeComponent();        
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            LoadCustomer(new Guid(this.NavigationContext.QueryString.Where(x => x.Key == "id").Single().Value));
            base.OnNavigatedTo(e);
        }

        private void LoadCustomer(Guid customerId)
        {
            CustomerRepository repository = new CustomerRepository();
            Customer customer = repository.GetById(customerId);
            CustomerId.Text = customer.Id.ToString();
            FirstName.Text = customer.FirstName;
            LastName.Text = customer.LastName;
            HouseNumber.Text = customer.Address.HouseNumber.ToString();
            PostalCode.Text = customer.Address.PostCode;
            LandLineNumber.Text = customer.ContactNumbers.Where(x => x.ContactNumberType == ContactNumberType.Landline).Single().Number;
            MobileNumber.Text = customer.ContactNumbers.Where(x => x.ContactNumberType == ContactNumberType.Mobile).Single().Number;
        }

        private void UpdateCustomer(object sender, System.Windows.RoutedEventArgs e)
        {
            if (Validate())
            {
                CustomerRepository customerRepository = new CustomerRepository();

                Customer customer = new Customer()
                {
                    Id = new Guid(CustomerId.Text),
                    FirstName = FirstName.Text,
                    LastName = LastName.Text,
                    Address = new Address() { HouseNumber = int.Parse(HouseNumber.Text), PostCode = PostalCode.Text },
                };

                customer.ContactNumbers.Add(new ContactNumber() { Number = LandLineNumber.Text, ContactNumberType = ContactNumberType.Landline });
                customer.ContactNumbers.Add(new ContactNumber() { Number = MobileNumber.Text, ContactNumberType = ContactNumberType.Mobile });

                customerRepository.Update(customer);

                MessageBox.Show(string.Format("The customer {0} {1} has been updated successfully", FirstName.Text, LastName.Text), "Success", MessageBoxButton.OK);

                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            }
        }

        private void DeleteCustomer(object sender, System.Windows.RoutedEventArgs e)
        {
            CustomerRepository customerRepository = new CustomerRepository();
            customerRepository.Delete(new Guid(CustomerId.Text));

            MessageBox.Show("Customer successfully deleted");

            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        private bool Validate()
        {
            if (string.IsNullOrEmpty(FirstName.Text))
            {
                MessageBox.Show("First name cannot be empty");
                return false;
            }

            if (string.IsNullOrEmpty(LastName.Text))
            {
                MessageBox.Show("Last name cannot be empty");
                return false;
            }

            int houseNumber;
            if (string.IsNullOrEmpty(HouseNumber.Text) || int.TryParse(HouseNumber.Text, out houseNumber) == false)
            {
                MessageBox.Show("House number requires an integer");
                return false;
            }

            if (string.IsNullOrEmpty(PostalCode.Text))
            {
                MessageBox.Show("PostCode cannot be empty");
                return false;
            }

            if (string.IsNullOrEmpty(LandLineNumber.Text))
            {
                MessageBox.Show("Landline number cannot be empty");
                return false;
            }

            if (string.IsNullOrEmpty(MobileNumber.Text))
            {
                MessageBox.Show("MobileNumber cannot be empty");
                return false;
            }


            return true;
        }
    }
}